package testJdk.commonUse.string.regex;

import org.junit.Test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 * @version 1.0.0
 * Created by cyd on 2022-12-14 11:56:39
 * @ClassName : learn_jdk.commonUse.string.march.TestMatches
 * @Description : 类描述
 */
public class TestMatches {

    /**
     * 只能全量匹配
     */
    @Test
    public void testMatches() {
        String currentStr = "123456789";
        // 只能全量匹配
        boolean matches = Pattern.matches("123456789", currentStr);
        boolean matches2 = Pattern.matches("123", currentStr);

        // 可以部分匹配
        boolean matches3 = Pattern.matches(".*123.*", currentStr);

        System.out.println(matches);
        System.out.println(matches2);
        System.out.println(matches3);
    }

    @Test
    public void testContain() {
        String currentStr = "123456789";
        System.out.println(currentStr.contains("123"));
    }

    /**
     * 匹配数字并转化
     * 通过捕获组将一段字符串分割成四份
     */
    @Test
    public void test() {
        String reg = "(\\d+):(\\d+):(\\d+).(\\d+)";
        String log = "01:1:02.211";
        Matcher matcher = Pattern.compile(reg).matcher(log);
        if (matcher.find()) {
            System.out.println(matcher.group(1));
            System.out.println(matcher.group(2));
            System.out.println(matcher.group(3));
            System.out.println(matcher.group(4));
            long H = Long.parseLong(matcher.group(1)) * 60 * 60 * 1000;
            long M = Long.parseLong(matcher.group(2)) * 60 * 1000;
            long S = Long.parseLong(matcher.group(3)) * 1000;
            long N = Long.parseLong(matcher.group(4));
            System.out.println(H + M + S + N);
        }else {
            System.out.println("no result");
        }
    }
}
